const mongoose = require('mongoose');
const { ObjectId } = mongoose.Schema;

const locationSchema = mongoose.Schema({
  begin: {
    row: Number,
    column: Number,
  },
  end: {
    row: Number,
    column: Number,
  },
}, { _id: false });

// 信息流追踪:跟踪信息在程序中的传播路径，识别信息从源（输入）到汇（输出）的流动情况
const PathTrackerSchema = mongoose.Schema({
  varialeName: String,
  paths: [
    [
      {
        fileId: ObjectId,
        range: locationSchema,
      },
    ],
  ],
}, { _id: false });

// 数据依赖分析:分析数据之间的依赖关系
const DependencyAnalyzerSchema = mongoose.Schema({
  [String]: {
    conditionalDependStrongly: [String],
    conditionalDependWeakly: [String],
    directDependStrongly: [String],
    directDependWeakly: [String],
    definitionType: {
      type: String,
      enum: ['strong', 'weak'],
    },
  },
}, { _id: false });

// 信息流统计分析:统计分析信息流动的频率（使用该信息的路径数量占所有路径数量的比例）、路径长度（路径中使用该信息的节点数的平均值）、数据传输量（信息的大小）等
const StatisticsSchema = mongoose.Schema({
  [String]: {
    dataVolume: Number,
    frequency: String,
    pathLen: Number,
  },

}, { _id: false });

// 数据对象属性分析:列出每个数据对象的属性，包括属性名、类型和可见性
const DateObjectAnalyzerSchema = mongoose.Schema({
  objectName: String,
  attributes: {
    [String]: {
      attrType: String,
      attrValue: String,
      attrVisibility: {
        type: String,
        eumn: ['public', 'private', 'protected'],
      },
    },
  },
  kind: String,
}, { _id: false });

// 变量生命周期分析:分析变量的生命周期，从定义到最后一次使用
// 变量范围分析:分析变量的作用域和范围，识别全局变量和局部变量的使用情况
const LifetimeAnalyzerSchema = mongoose.Schema({
  variableName: String,
  scope: {
    type: String,
    eumn: ['file', 'function', 'project'],
  },
  functionId: ObjectId,
  fileId: ObjectId,
  storage: {
    type: String,
    eumn: ['static_local', 'local', 'global'],
  },
  lifetime: locationSchema,
}, { _id: false });

module.exports = {
  creatorId: ObjectId,
  projectId: ObjectId,
  versionId: { type: ObjectId, index: true },
  fileId: { type: ObjectId, index: true },
  functionId: String,
  functionMangledId: String,
  ruleType: {
    type: String,
    eumn: ['PathTracker', 'DependencyAnalyzer', 'Statistics', 'DateObjectAnalyzer', 'LifetimeAnalyzer'],
  },
  data: PathTrackerSchema | DependencyAnalyzerSchema | StatisticsSchema | DateObjectAnalyzerSchema | LifetimeAnalyzerSchema,
};
